<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("components4phpfull/jtdivwindow.inc.php");
use_unit("components4phpfull/jtiframe.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class vendor_edit extends Page
{
       public $lblAddress = null;
       public $Label14 = null;
       public $edAreaFax = null;
       public $lblCity = null;
       public $Label31 = null;
       public $cmbCountry = null;
       public $Label18 = null;
       public $edCity = null;
       public $Label13 = null;
       public $btnShowProduct = null;
       public $lblarea = null;
       public $edArea = null;
       public $Label11 = null;
       public $lblphone = null;
       public $edCompPhone = null;
       public $Label4 = null;
       public $JSProvinceMunicipality = null;
       public $JSRegionProvince = null;
       public $edOtherProd = null;
       public $Label6 = null;
       public $lblem = null;
       public $lblvname = null;
       public $Label9 = null;
       public $edCompEmail = null;
       public $edCompFax = null;
       public $Label17 = null;
       public $Label16 = null;
       public $cmbMunicipality = null;
       public $Label15 = null;
       public $cmbProvince = null;
       public $Label5 = null;
       public $cmbRegion = null;
       public $chkinactive = null;
       public $Label7 = null;
       public $cmbProduct = null;
       public $edBldg = null;
       public $Label3 = null;
       public $edAddress = null;
       public $Label2 = null;
       public $edVendorname = null;
       public $Label1 = null;
       public $edVendornum = null;
       public $lblVendorNum = null;
       public $hfError = null;
       public $JSValidate = null;
       public $JTDivProduct = null;
       public $JTGroupBox4 = null;
       public $btnAddProduct = null;
       public $btnDelProduct = null;
       public $edProduct = null;
       public $listProduct = null;
       public $compid = null;
       public $JTJavaScript1 = null;
       public $JTJavaScript2 = null;
       public $JTJavaScript3 = null;
       public $JTBdate = null;
       public $imgBack = null;
       public $JTDivWindow1 = null;
       public $btnCancelContact = null;
       public $btnSubmitContact = null;
       public $edEmail = null;
       public $Label30 = null;
       public $edFax = null;
       public $Label29 = null;
       public $Label28 = null;
       public $cmbContactMunicipality = null;
       public $Label27 = null;
       public $cmbContactProvince = null;
       public $Label26 = null;
       public $cmbContactRegion = null;
       public $edContactBldg = null;
       public $Label25 = null;
       public $edContactAddress = null;
       public $Label24 = null;
       public $edMobile = null;
       public $Label23 = null;
       public $edPhone = null;
       public $Label22 = null;
       public $Label21 = null;
       public $edContactDesignation = null;
       public $Label20 = null;
       public $edContactLname = null;
       public $Label19 = null;
       public $edContactMname = null;
       public $Label12 = null;
       public $edContactFname = null;
       public $Label10 = null;
       public $Label8 = null;
       public $JTContactList = null;
       public $Button3 = null;
       public $JTSiteTheme1 = null;
       public $hiddenVendor = null;
       public $Image1 = null;
       public $btnCancel = null;
       public $btnUpdate = null;
       function edCityJSBlur($sender, $params)
       {

       ?>
       validateRequire('edCity','lblCity');
       <?php

       }

       function edAddressJSBlur($sender, $params)
       {

       ?>
       validateRequire('edAddress','lblAddress');
       <?php

       }

       function edAreaJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       var edArea = document.getElementById('edArea').value;

       if(edArea.length>0)
       {
         regex_pattern = "^[0-9]{2,4}$";
         var reg = new RegExp(regex_pattern);
         var match = document.getElementById('edArea').value.match(reg);
         if(match==null)
         {
          document.getElementById('lblarea').innerHTML = "Incorrect format - e.g. 02&nbsp;&darr;";
         }
         else
         {
          document.getElementById('lblarea').innerHTML = "&nbsp;";
         }
       }
       else
       {
         document.getElementById('lblarea').innerHTML = "&darr; This field is required &darr;";
       }
       <?php

       }


        function edCompEmailJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       //validateRequire('edCompEmail','lblem');

       var edCompEmail = document.getElementById('edCompEmail').value;

       if(edCompEmail.length>0)
       {
       regex_pattern = "[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$";
       var regm = new RegExp(regex_pattern);
       var match = document.getElementById('edCompEmail').value.match(regm);
       if(match==null)
       {
        document.getElementById('lblem').innerHTML = "&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;";
       }
       }
       <?php

       }

       function edCompPhoneJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edCompPhone','lblphone');
       var edCompPhone = document.getElementById('edCompPhone').value;

       if(edCompPhone.length>0)
       {
       regex_pattern = "^[0-9]{3}[-]?[0-9]{4}$";
       var reg = new RegExp(regex_pattern);
       var match = document.getElementById('edCompPhone').value.match(reg);
       if(match==null)
       {
        document.getElementById('lblphone').innerHTML = "&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;";
       }
       else
       {
        document.getElementById('lblphone').innerHTML = "";
       }
       }
       <?php

       }


       function edVendornameJSBlur($sender, $params)
       {

       ?>
       //Add your javascript code here
       validateRequire('edVendorname','lblvname');
       <?php

       }


       function btnDelProductJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var ProductIndex = document.getElementById('listProduct').selectedIndex;
       var ProductValue = document.getElementById('listProduct')[ProductIndex].value;
       var comp = document.getElementById('compid').value;
       AjaxProduct3(ProductValue,comp);
       <?php

       }

       function btnAddProductJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var addproduct = document.getElementById('edProduct').value;
       var compider = document.getElementById('compid').value;
       AjaxProduct2(addproduct,compider);
       document.getElementById('edProduct').value = "";
       <?php

       }

       function btnShowProductJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivProduct').Show();
       document.getElementById('edProduct').value = "";
       <?php

       }

       function imgBackClick($sender, $params)
       {
       redirect("vendor_list.php?vend_id=".$this->hiddenVendor->getValue());
       }

       function imgBackJSMouseOut($sender, $params)
       {

       ?>
       //Add your javascript code here
        document.images['imgBack'].src="image/btn_back.png"
       <?php

       }

       function imgBackJSMouseOver($sender, $params)
       {

       ?>
       //Add your javascript code here
        document.images['imgBack'].src="image/btn_back2.png"
       <?php

       }

       function cmbContactProvinceJSChange($sender, $params)
       {
       echo $this->cmbContactMunicipality->ajaxCall("ProvinceChange");
       ?>
       //Add your javascript code here
       return false;
       <?php

       }

       function cmbContactRegionJSChange($sender, $params)
       {
       echo $this->cmbContactProvince->ajaxCall("RegionChange");
       ?>
       return false;
       <?php

       }

       function cmbProvinceJSChange($sender, $params)
       {
       //echo $this->cmbMunicipality->ajaxCall("VendorProvinceChange");
       ?>
       provinceMunicipality();
       return false;
       <?php

       }

       function cmbRegionJSChange($sender, $params)
       {
       //echo $this->cmbProvince->ajaxCall("VendorRegionChange");
       ?>
       regionProvince();
       return false;
       <?php

       }

       function JTDivWindow1Show($sender, $params)
       {
       $this->edContactFname->Text = '';
       $this->edContactMname->Text = '';
       $this->edContactLname->Text = '';
       $this->edContactDesignation->Text = '';
       $this->edPhone->Text = '';
       $this->edMobile->Text = '';
       $this->edContactAddress->Text = '';
       $this->edContactBldg->Text = '';
       $this->edFax->Text = '';
       $this->edEmail->Text = '';


       }

       function Button1JSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       var vendor = document.getElementById('hiddenVendor').value;
       window.open("vendor_contact_create.php?vend_id="+vendor,"Window1",  "menubar=no,width=790,height=400,left=300,top=230,toolbar=no");
       return false;
       <?php

       }

       function btnCancelContactJSClick($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.getElementById('JTDivWindow1').Hide();
       return false;
       <?php

       }



       function insertContact()
       {
       mysql_query("INSERT INTO vendor_contact(company_id,
       vendor_num,
       fname,
       mname,
       lname,
       designation,
       bdyear,
       bdday,
       bdmonth,
       phone,
       mobile,
       address,
       bldg,
       region,
       province,
       municipality,
       fax,
       email)

       VALUES
       ('".$_SESSION['company_id']."',
       '".$this->hiddenVendor->getValue()."',
       '".$this->edContactFname->Text."',
       '".$this->edContactMname->Text."',
       '".$this->edContactLname->Text."',
       '".$this->edContactDesignation->Text."',
       '".$this->cmbyear->getItemIndex()."',
       '".$this->cmbDay->getItemIndex()."',
       '".$this->cmbMonth->getItemIndex()."',
       '".$this->edPhone->Text."',
       '".$this->edMobile->Text."',
       '".$this->edContactAddress->Text."',
       '".$this->edContactBldg->Text."',
       '".$this->cmbContactRegion->getItemIndex()."',
       '".$this->cmbContactProvince->getItemIndex()."',
       '".$this->cmbContactMunicipality->getItemIndex()."',
       '".$this->edFax->Text."',
       '".$this->edEmail->Text."')") or die("INSERT CONTACT - ".mysql_error());


       }





       function btnSubmitContactJSClick($sender, $params)
       {
       echo $this->btnSubmitContact->ajaxCall("insertContact");
       ?>
       alert('Added contact successfully');
       document.getElementById('JTDivWindow1').Hide();
       document.getElementById("JTContactList").src="vendor_contact_list.php?company_id=+$_SESSION['company_id']&vendor_id=+$this->hiddenVendor->getValue()"
       return(false);
       <?php
       }

       function btnCancelClick($sender, $params)
       {
       redirect("vendor_list.php?vend_id=".$this->hiddenVendor->getValue());
       }

       function btnUpdateClick($sender, $params)
       {
       $this->validateRequired($this->edVendorname->Text, 'lblvname');
       $this->validateRequired($this->edCity->Text, 'lblCity');
       $this->validateRequired($this->edAddress->Text, 'lblAddress');


       $phone = $this->edCompPhone->Text;
       $this->validateRequired($this->edCompPhone->Text, 'lblphone');

       if($phone!='')
       {
       if(preg_match("/^[0-9]{3}[-]?[0-9]{4}$/", $phone))
       {
       $this->lblphone->Caption = '';
       }
       else
       {
       $this->lblphone->Caption = '&darr;&nbsp;Incorrect format - e.g.899-1234&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $acode = $this->edArea->Text;
       if($acode!='')
       {
       if(preg_match("/^[0-9]{2,4}$/", $acode))
       {
       $this->lblarea->Caption = '';
       }
       else
       {
       $this->lblarea->Caption = 'Incorrect format - e.g.02&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $this->validateRequired($this->edArea->Text, 'lblarea');

       $email = $this->edCompEmail->Text;
       /*if($email=='')
       $this->validateRequired($this->edCompEmail->Text, 'lblem');*/

       if($email!='')
       {
       if(preg_match("/[A-Za-z0-9_\.]+[@][A-Za-z-]+[.][A-Za-z]{2,4}([\.A-Za-z]{2,3})?$/", $email))
       {
       $this->lblem->Caption = '';
       }
       else
       {
       $this->lblem->Caption = '&darr;&nbsp;Incorrect format - e.g.(user@mail.com)&nbsp;&darr;';
       $this->hfError->setValue('1');
       }
       }

       $error = $this->hfError->getValue();
       if($error == '1' || $number != $key)
       {
          $this->hfError->setValue('0');
          return false;
       }
       else if($error == '0')
       {


       if($this->chkinactive->Checked==true)
       {
       $inactive = '1';
       }
       else
       {
       $inactive = '0';
       }
       /*
       region = '".$this->cmbRegion->getItemIndex()."',
       province = '".$this->cmbProvince->getItemIndex()."',
       municipality = '".$this->cmbMunicipality->getItemIndex()."',
       */
       $query_update = "UPDATE vendor_tbl SET
       vendor_num ='".$this->edVendornum->Text."',
       vendor_name = '".$this->edVendorname->Text."',
       address = '".$this->edAddress->Text."',
       bldg = '".$this->edBldg->Text."',
       city = '".$this->edCity->Text."',
       country = '".$this->cmbCountry->getItemIndex()."',
       product = '".$this->cmbProduct->getItemIndex()."',
       others = '".$this->edOtherProd->Text."',
       comp_phone = '".$this->edCompPhone->Text."',
       acode = '".$this->edArea->Text."',
       fax_acode = '".$this->edAreaFax->Text."',
       fax_num = '".$this->edCompFax->Text."',
       email = '".$this->edCompEmail->Text."',
       inactive_vendor = '".$inactive."'
       where vendor_id='".$this->hiddenVendor->getValue()."'";
       mysql_query($query_update) or die(mysql_error());


       redirect("vendor_list.php?vend_id=".$this->hiddenVendor->getValue());

       }
       }

       function validateRequired($value, $lbl)
       {
          if($value == '' || $value == null)
          {
             $this->$lbl->Caption = '&darr; This field is required &darr;';
             $this->hfError->setValue('1');
          }
       }

       function vendor_editCreate($sender, $params)
       {
       $this->hfError->setValue('0');
       $this->lblphone->Caption = '';
       $this->lblarea->Caption = '';
       $this->lblvname->Caption = '';
       $this->lblem->Caption = '';
       $this->lblCity->Caption  = '';
       $this->lblAddress->Caption  = '';


       $this->compid->Value = $_SESSION['company_id'];
       $vend = $_GET['vend_id'];
       $this->hiddenVendor->setValue($vend);

       $query1 = "select * from vendor_tbl where vendor_id='".$this->hiddenVendor->getValue()."'";
       $result = mysql_query($query1)or die(mysql_error());
       $row = mysql_fetch_array($result);

       $this->edVendornum->Text = $row['vendor_num'];
       $this->edVendorname->Text = $row['vendor_name'];
       $this->edAddress->Text = $row['address'];
       $this->edBldg->Text = $row['bldg'];
       $this->edOtherProd->Text = $row['others'];
       $this->edCompPhone->Text = $row['comp_phone'];
       $this->edArea->Text = $row['acode'];
       $this->edCompFax->Text = $row['fax_num'];
       $this->edCompEmail->Text = $row['email'];

       $this->edAreaFax->Text = $row['fax_acode'];
       $this->edCity->Text = $row['city'];

       $this->cmbCountry->Clear();
       $query_country = "SELECT country FROM countries";
       $result_country = mysql_query($query_country) or die("SELECT COUNTRIES ".mysql_error());
       while($row_country = mysql_fetch_array($result_country))
       {
        $country = $row_country['country'];
        $this->cmbCountry->AddItem($country, '', $country);
       }

       $this->cmbCountry->setItemIndex($row['country']);

        $this->cmbProduct->Clear();
        $this->cmbProduct->AddItem('--Select--', null, null);
        $this->cmbProduct->ItemIndex=$row['product'];
        $result_style = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

        while($row_style = mysql_fetch_array($result_style))
        {
            $this->cmbProduct->AddItem($row_style['value'], null, $row_style['value']);
        }

        //list products and services
        $this->listProduct->Clear();
        $result_product = mysql_query("select * from lookup_inventory_tbl where company_id = '".$_SESSION['company_id']."' AND category='types'");

        while($row_product = mysql_fetch_array($result_product))
        {
            $this->listProduct->AddItem($row_product['value'], null, $row_product['value']);
        }

        $inactive = $row['inactive_vendor'];

        if($inactive=="1")
        {
        $this->chkinactive->Checked=true;
        }
        else
        {
        $this->chkinactive->Checked=false;
        }

        $this->JTContactList->setURL('vendor_contact_list.php?company_id='.$_SESSION['company_id'].'&vendor_id='.$this->hiddenVendor->getValue());


       //Contact region,prov,municipality

       $this->cmbContactRegion->Clear();
       $this->cmbContactRegion->AddItem('--Select--', null, null);
       $result_region = mysql_query("select * from lookup_region");

        while($row_region = mysql_fetch_array($result_region))
        {
            $this->cmbContactRegion->AddItem($row_region['region'], null, $row_region['id']);
        }
        $this->cmbContactProvince->Clear();
        $this->cmbContactMunicipality->Clear();
        $this->cmbContactProvince->AddItem('--Select Region First--',null,null);
        $this->cmbContactMunicipality->AddItem('--Select Province First--',null,null);




       //Vendor Region,Prov, Municipality

       $this->cmbRegion->Clear();
       $this->cmbRegion->AddItem('--Select--', null, null);
       $this->cmbRegion->ItemIndex=$row['region'];
       $result_vend_region = mysql_query("select * from lookup_region");

        while($row_vend_region = mysql_fetch_array($result_vend_region))
        {
            $this->cmbRegion->AddItem($row_vend_region['region'], null, $row_vend_region['id']);
        }

       $region_id = $row['region'];
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $row['province'];
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }
        $this->cmbProvince->setItemIndex($row['province']);
        $this->cmbMunicipality->setItemIndex($row['municipality']);
       }

       function RegionChange()
       {
       $region_id = $this->cmbContactRegion->getItemIndex();
       $this->cmbContactProvince->Clear();
       $this->cmbContactProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbContactProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbContactProvince->Clear();
        $this->cmbContactMunicipality->Clear();
        $this->cmbContactProvince->AddItem('--Select Region First--',null,null);
        $this->cmbContactMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function ProvinceChange()
       {
       $region_id = $this->cmbContactRegion->getItemIndex();
       $this->cmbContactProvince->Clear();
       $this->cmbContactProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbContactProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbContactProvince->getItemIndex();
       $this->cmbContactMunicipality->Clear();
       $this->cmbContactMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbContactMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbContactMunicipality->Clear();
        $this->cmbContactMunicipality->AddItem('--Select Province First--',null,null);
       }
       }





       function VendorRegionChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       if($region_id==0)
       {
        $this->cmbProvince->Clear();
        $this->cmbMunicipality->Clear();
        $this->cmbProvince->AddItem('--Select Region First--',null,null);
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }


       function VendorProvinceChange()
       {
       $region_id = $this->cmbRegion->getItemIndex();
       $this->cmbProvince->Clear();
       $this->cmbProvince->AddItem('--Select--', null, null);
       $result_province = mysql_query("select * from lookup_province where region_id='".$region_id."'");

       while($row_province = mysql_fetch_array($result_province))
       {
            $this->cmbProvince->AddItem($row_province['province'], null, $row_province['id']);
       }

       $province_id = $this->cmbProvince->getItemIndex();
       $this->cmbMunicipality->Clear();
       $this->cmbMunicipality->AddItem('--Select--', null, null);
       $result_municipality = mysql_query("select * from lookup_municipality where province_id='".$province_id."'");

       while($row_municipality = mysql_fetch_array($result_municipality))
       {
            $this->cmbMunicipality->AddItem($row_municipality['municipality'], null, $row_municipality['id']);
       }

       if($province_id==0)
       {
        $this->cmbMunicipality->Clear();
        $this->cmbMunicipality->AddItem('--Select Province First--',null,null);
       }
       }

}

global $application;

global $vendor_edit;

//Creates the form
$vendor_edit=new vendor_edit($application);

//Read from resource file
$vendor_edit->loadResource(__FILE__);

//Shows the form
$vendor_edit->show();

?>